package zhoug.zui.dialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import zhoug.zui.R;

import androidx.annotation.NonNull;

/**
 * 正在加载圆形窗口
 *设置全局样式
 *  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 *      <item name="zui_style_ZUILoadingDialog">@style/style_ZUILoadingDialog</item>
 * </style>
 *<style name="style_ZUILoadingDialog" parent="zui_style_ZUILoadingDialog">
 *         <item name="android:layout_width">30dp</item>
 *         <item name="android:layout_height">30dp</item>
 *         <item name="zui_loadingDrawable">@drawable/test_loading</item>
 *</style>
 *
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
public class ZUILoadingDialog extends ZUIDialog {
    private static final String TAG = ">>>ZUILoadingDialog";
    private static final boolean DEBUG = false;
    private ProgressBar mProgressBar;
    private Drawable mLoadingDrawable;

    public ZUILoadingDialog(@NonNull Context context) {
        super(context,0,R.attr.ZUILoadingDialog_theme, R.style.ZUILoadingDialog_def);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZUILoadingDialog, defStyleAttr, defStyleRes);
        int width = a.getLayoutDimension(R.styleable.ZUILoadingDialog_android_layout_width, WindowManager.LayoutParams.WRAP_CONTENT);
        int height = a.getLayoutDimension(R.styleable.ZUILoadingDialog_android_layout_height, WindowManager.LayoutParams.WRAP_CONTENT);
        mLoadingDrawable = a.getDrawable(R.styleable.ZUILoadingDialog_zui_loadingDrawable);
        setAttributes(width, height, Gravity.CENTER);
        a.recycle();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zui_loading_dialog;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        mProgressBar = findViewById(R.id.progressBar);
        ViewGroup.LayoutParams layoutParams = mProgressBar.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = getWidth();
            layoutParams.height = getHeight();
            mProgressBar.setLayoutParams(layoutParams);
        }
        if (mLoadingDrawable != null) {
            mProgressBar.setIndeterminateDrawable(mLoadingDrawable);
        }

        Window window = getWindow();
        if (window != null) {
            if (DEBUG) {
                Log.d(TAG, "init:去掉背景");
            }
            window.setBackgroundDrawableResource(R.drawable.zui_transparent);
        }
    }

    public void setLoadingDrawable(Drawable mLoadingDrawable) {
        this.mLoadingDrawable = mLoadingDrawable;
    }
}
